package  com.wsu.search.dingshiqi;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletRequest;

import com.wsu.search.timer.MyTimer;
import com.wsu.search.timer.TimerInterface;
import com.wsu.search.tools.GetProperties;
import com.wsu.search.tools.GetUrl;
import com.wsu.search.tools.Tools;
/**
 * **
 * @author Administrator
 *
 */
public class Time implements TimerInterface {

	public Time() {

	}
	public void startTimer() {

		MyTimer timer = new MyTimer();
		Calendar cldr = Calendar.getInstance();
		cldr.set(cldr.get(Calendar.YEAR), cldr.get(Calendar.MONTH), cldr.get(Calendar.DAY_OF_MONTH), 2, 10); // 运行时间 运行格式hh:mm
		timer.setUpdateCalendar(cldr);
		timer.setSecond(60 * 60); // 单位是秒60 * 60 * 24 一天
		timer.setTimerInterface(this); //
		timer.run(); // 启动定时器程序
	}

	public void run() {

		try {
		
	
			//半夜定时做索引更新
			String nowtime = Tools.getNewTime();
			int hh = Integer.parseInt(nowtime.substring(11, 13));
			int mm = Integer.parseInt(nowtime.substring(14, 16));
			
			String dsqurl = "";
			GetProperties getProps = new GetProperties(getClass().getResource("/web.properties").getPath());
			dsqurl = getProps.getKeyValue("dsqurl");
			getProps.close();
			
			if(hh==3&&mm>49)	//hh==23&&mm>39
			{
				//new Tools().ResetLog();
				//System.out.println("Index is Reset:" + nowtime+" ");
				//new Search().index();
				//System.out.println("Index is Reset OK! " + nowtime+"");
			}else
			{
				//定时更新 改为触发更新
				//new Search().index();
				GetUrl gg = new GetUrl();
				gg.getHtml(dsqurl);
				//System.out.println("执行"+dsqurl);
				
				
				Date endTime = new Date();
				/*
				System.out
				        .println("UpdateIndex use  "
				                + ((endTime.getTime() - startTime.getTime()) / 1000.00)
				                + "s ");
				
				System.out.println("UpdateIndexTime:" + nowtime+"");
				*/
				
			}

		} catch (Exception ex) {
			System.out.println(ex);
		}

	}

	private ServletRequest getServletContext()
    {
	    // TODO 自动生成方法存根
	    return null;
    }

}
